package com.mit.wx.service.impl;

import com.mit.wx.dao.BaseDao;
import com.mit.wx.dao.MaterialDao;
import com.mit.wx.wx.dto.MaterialDescribe;
import com.mit.wx.wx.dto.WxMaterialCreateResponse;
import com.mit.wx.wx.dto.WxNewsItem;
import com.mit.wx.wx.dto.WxNewsList;
import com.mit.wx.entity.AccessToken;
import com.mit.wx.entity.Material;
import com.mit.wx.service.AccessTokenService;
import com.mit.wx.service.MaterialService;
import com.mit.wx.vo.EasyUIDataGrid;
import com.mit.wx.wx.WxMaterialApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 2018/10/12 Create by 郭文梁
 * MaterialServiceImpl
 * 素材服务类实现
 *
 * @author 郭文梁
 * @date 2018/10/12
 */
@Service
@Slf4j
public class MaterialServiceImpl extends BaseServiceImpl<Material> implements MaterialService {
    private final MaterialDao materialDao;
    private final AccessTokenService accessTokenService;

    @Autowired
    public MaterialServiceImpl(MaterialDao materialDao, AccessTokenService accessTokenService) {
        this.materialDao = materialDao;
        this.accessTokenService = accessTokenService;
    }

    @Override
    public BaseDao<Material> getRepository() {
        return materialDao;
    }

    @Override
    public Material create(Material material, MultipartFile file) throws IOException {
        AccessToken accessToken = accessTokenService.getAccessToken();

        byte[] content = file.getBytes();
        String contentType = file.getContentType();
        String filename = file.getOriginalFilename();
        MaterialDescribe describe = new MaterialDescribe();
        describe.setTitle(material.getTitle());
        describe.setIntroduction(material.getIntroduction());
        //请求API
        WxMaterialCreateResponse response = WxMaterialApi.createMaterial(accessToken.getAccessToken(), material.getType(), content, contentType, filename, describe);

        if (response.getMediaId() != null) {
            //操作成功
            material.setUrl(response.getUrl());
            material.setMediaId(response.getMediaId());
            material.setCreateAt(new Date());
            //保存到数据库
            return save(material);
        } else {
            //操作失败
            log.error("Could not create material,because response = {}", response);
            return null;
        }
    }

    @Override
    public EasyUIDataGrid<WxNewsItem> getNewsList(int offset, int size) throws IOException {
        AccessToken accessToken = accessTokenService.getAccessToken();
        assert accessToken != null : "Please Waiting for AccessToken init success";
        WxNewsList newsList = WxMaterialApi.getNewList(accessToken.getAccessToken(), offset, size);
        if (newsList == null) {
            return null;
        }
        long totalCount = newsList.getTotalCount();
        List<WxNewsItem> item = newsList.getItem();
        return EasyUIDataGrid.of(totalCount, item);
    }
}
